package com.jdk.learn.nio._04;

import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

/**
 * Created by ricky
 * on 2018/2/11.

 单向管道
 */
public class TestPipe {

    @Test
    public void test() throws IOException {
        Pipe pipe= Pipe.open();
        ByteBuffer buffer=ByteBuffer.allocate(1024);

        //将缓冲区数据写入管道
        Pipe.SinkChannel sink = pipe.sink();
        buffer.put("单向管道发送数据".getBytes());
        buffer.flip();
        sink.write(buffer);
        //将缓冲区数据读取到管道
        Pipe.SourceChannel source = pipe.source();
        buffer.flip();
        int len=source.read(buffer);
        System.out.println(new String(buffer.array(),0,len));

        sink.close();
        source.close();
    }
}
